/*
Author: Jason Bareswilt
Functionality: Create the main menu for the game
Version number: 0.0.9
Testers: needs to be tested
Date written: November 22nd 2011
Date tested: needs to be tested
*/
package disease;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import disease.GameMap;
import disease.Instructions;
import disease.Options;

public class RottingFlesh
{
	public static int difficulty = 2; //Initial difficulty setting of normal
	public static String GameName = ""; //Initialize name
	public static JFrame frame = new JFrame("Rotting Flesh"); //Initialize main frame
	public DrawingArea drawingArea = new DrawingArea(); //Initialize 2D Graphics Area
		
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable() 
		{
			//Starts thread for the main menu GUI
			public void run() 
			{
				Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
				frame.setLocation((screen.width / 2) - 200, (screen.height / 2) - 200);
				askForName(screen);
			}
		});
	}
	
	static void askForName(Dimension screen)
	{		
		//function askForName creates a small GUI that asks for the display name
		//of the player
		final JFrame nameFrame = new JFrame("Get name");
		nameFrame.getContentPane().removeAll();
		nameFrame.getContentPane().removeAll();
		nameFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		nameFrame.setLayout(new GridLayout(2,2));
		JButton Submit = new JButton("Submit");
		JButton Quit = new JButton("Quit");
		final JLabel NameLabel = new JLabel("Enter Name:");
		final JTextField NameText = new JTextField();
		Submit.setBackground(Color.RED);
		Quit.setBackground(Color.RED);
		nameFrame.getContentPane().add(NameLabel);
		nameFrame.getContentPane().add(NameText);
		nameFrame.getContentPane().add(Submit);
		nameFrame.getContentPane().add(Quit);
		nameFrame.setLocation((screen.width / 2) - 200, (screen.height / 2) - 200);
		nameFrame.setVisible(true);
		nameFrame.setSize(250, 100);
		
		//If submit button is pressed:
		Submit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				GameName = NameText.getText();
				System.out.println(GameName.length());
				
				//Checks to make sure that display name only consists of letters between
				//one and ten characters
				if (GameName.length() < 1 || GameName.length() > 10 || !GameName.matches("[a-zA-Z]+"))
				{
					nameFrame.dispose();
					JFrame parent = new JFrame();
				    JOptionPane optionPane = new JOptionPane("Invalid name, please try again\n" +
				    		"Only letters A-Z allowed", 
				    		JOptionPane.WARNING_MESSAGE);
				    JDialog dialog = optionPane.createDialog(parent, "Warning");
				    dialog.setVisible(true);
				    main(null);
				}
				else
				{
					//Removes the name GUI and calls the main menu for the game
					nameFrame.dispose();
					createAndShowGUI();
				}
			}
		});
		
		//If Quit button is pressed:
		Quit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
	}

	static void createAndShowGUI()
	{
		//Displays the main graphical interface for our main menu with buttons to
		//go to options, instructions, and game
		System.out.println("Rotting Flesh");
		DrawingArea drawingArea = new DrawingArea();
		ButtonPanel buttonPanel = new ButtonPanel( drawingArea );
		frame.getContentPane().removeAll();
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		DrawingArea panel = new DrawingArea(new ImageIcon("ZombiesRevBMainMenu.jpg").getImage());
		frame.getContentPane().add(panel);
		frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		frame.setSize(450, 450);
		frame.setResizable(false);
		frame.setVisible(true);
	}
	
	static class ButtonPanel extends JPanel implements ActionListener
	{
		//Button Panel Class that defines placement, color, and actions of buttons
		private static final long serialVersionUID = 1L;
		public ButtonPanel(DrawingArea drawingArea)
		{
			//creates buttons and sets colors of the buttons and of panel background
			add( createButton("Play", Color.RED));
			add( createButton("Instructions", Color.RED));
			add( createButton("Options", Color.RED));
			add( createButton("High Scores", Color.RED));
			add( createButton("Quit", Color.RED));
			setBackground(Color.BLACK);
		}

		private JButton createButton(String text, Color background)
		{
			//Does the physical creationg of each button
			JButton button = new JButton(text);
			button.setBackground(background);
			button.addActionListener(this);
			UIManager.put("Button.select", Color.red);
			updateUI();
			return button;
		}

		public void actionPerformed(ActionEvent e)
		{
			//Action Listener for each button, goes to a new class depending on
			//the button pressed. 
			if ("Play".equals(e.getActionCommand()))
			{
				SwingUtilities.invokeLater(new Runnable()
		        {
		            public void run()
		            {
		                new GameMap().gameMapGUI(GameName,difficulty);
		            }
		        });				
			}
			if ("Instructions".equals(e.getActionCommand()))
			{
				Instructions instructions = new Instructions();
				instructions.instructionsGUI();
			}
			if ("Options".equals(e.getActionCommand()))
			{
				Options options = new Options();
				options.optionsGUI();
			}
			if ("High Scores".equals(e.getActionCommand()))
			{
				HighScores.HighScoresGUI();
			}
			if ("Quit".equals(e.getActionCommand()))
			{
				System.exit(0);
			}
		}
	}

	static class DrawingArea extends JPanel
	{
		//Drawing area class that forms the main area of the GUI
		private static final long serialVersionUID = 1L;
		private static Image img;
		
		public DrawingArea(String filename)
		{
			//Sets background image to the image specified in main GUI class
			this(new ImageIcon(img).getImage());
		}

		public DrawingArea(Image img)
		{
			//Sets size of given image to fit the size of the frame
			DrawingArea.img = img;
			Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
			setPreferredSize(size);
			setMinimumSize(size);
			setMaximumSize(size);
			setSize(size);
			setLayout(null);
	    }
		
		//empty constructor class
		public DrawingArea(){}

		public void paintComponent(Graphics g)
		{
			//Draws the background image in the drawing area
			g.drawImage(img, 0, 0, null);
		}
	}
}